package com.zhc.dejingapi.pojo.qingflow.downdata;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class QingDownDataDetailDBModel implements Serializable {
    //名	类型	长度	小数点	不是 null	虚拟	键	虚拟类型	表达式	枚举值	默认值	注释	存储	列格式	字符集	排序规则	键长度	键排序	永远生成	根据当前时间戳更新	二进制	自动递增	无符号	填充零
    //detail_id	bigint			true	false	true										0	ASC	false	false	false	true	false	false
    private long detail_id;
    //main_id	bigint
    private long main_id;
    //mainTblFieldName	varchar	60
    private String mainTblFieldName;
    //detailTableName	varchar	80
    private String detailTableName;
    //isNeedSyn	int
    private int  isNeedSyn;

    //private String strFieldList="qingApplyId";
    @Getter
    @Setter
    private StringBuilder strFieldNameBuilder=new StringBuilder();
    //private String strFieldPara="?";
    @Getter
    @Setter
    private StringBuilder strFieldParaBuilder=new StringBuilder();
    //private List<Object> ListParameter=null;
    @Getter
    @Setter
    private List<QingDownDataFieldDBModel> listFields;

    public void loadMainFields(JdbcTemplate mysqlJDBC){
        String strSql="SELECT a.details_id,a.detail_id,a.main_id,\n" +
                "       a.tableName,a.qingTitle,a.dbFieldName,a.dbFieldType\n" +
                "from qing_donwdatafields a\n" +
                " where a.main_id=? and a.detail_id=? \n" +
                "";
        listFields=mysqlJDBC.query(strSql,new BeanPropertyRowMapper<>(QingDownDataFieldDBModel.class),main_id,detail_id);

        if(listFields==null||listFields.isEmpty()){

        }
        else{
            mapFieldValue=new HashMap<>();
            mapFieldContrast=new HashMap<>();
            strFieldNameBuilder=new StringBuilder();
            strFieldParaBuilder=new StringBuilder();
            //strFieldList="qingApplyId";
            strFieldNameBuilder.append("main_id,qingApplyId ");
            //strFieldPara="?";
            strFieldParaBuilder.append("?,?");
            mapRowFieldValue=new HashMap<>();

            for (int i = 0; i < listFields.size(); i++) {
                mapFieldContrast.put(listFields.get(i).getQingTitle(),listFields.get(i).getDbFieldName());
                mapFieldValue.put(listFields.get(i).getDbFieldName(),null);
                //strFieldList += "," + listFields.get(i).getDbFieldName();
                strFieldNameBuilder.append("," + listFields.get(i).getDbFieldName());
                //strFieldPara += ",?";
                strFieldParaBuilder.append(",?");
            }
        }

    }


    @Getter
    @Setter
    private Map<String,Object > mapFieldValue=null;

    @Setter
    @Getter
    private Map<String,String > mapFieldContrast=null;

    @Setter
    @Getter
    private Map<Integer,Map<String,Object>> mapRowFieldValue=null;


    public void PutFieldValue(Integer iRowIndex, String strQueTitle,Object strFieldValue) throws Exception{

        //有行号。
        if (!mapRowFieldValue.containsKey(iRowIndex)){
            Map<String,Object> mapfieldItemValue=new HashMap<>();
            mapRowFieldValue.put(iRowIndex,mapfieldItemValue);
        }

        if(mapFieldContrast.containsKey(strQueTitle)){

            String strFieldName=mapFieldContrast.get(strQueTitle);
            mapRowFieldValue.get(iRowIndex).put(strFieldName,strFieldValue);
            //mapFieldValue.put(mapFieldContrast.get(strQueTitle),strFieldValue);
        }
        else{
            throw new Exception("明细表字段["+strQueTitle+"]不存在！");
        }
    }

    public void PutSelFieldValue(Integer iRowIndex, String strQueTitle,Object strFieldValue) throws Exception{

        //有行号。
        if (!mapRowFieldValue.containsKey(iRowIndex)){
            Map<String,Object> mapfieldItemValue=new HashMap<>();
            mapRowFieldValue.put(iRowIndex,mapfieldItemValue);
        }

        if(mapFieldContrast.containsKey(strQueTitle)){

            String strFieldName=mapFieldContrast.get(strQueTitle);
            mapRowFieldValue.get(iRowIndex).put(strFieldName,strFieldValue);
            //mapFieldValue.put(mapFieldContrast.get(strQueTitle),strFieldValue);
        }
        else{
            //throw new Exception("字段不存在！");
        }
    }
    public void SaveToDB(JdbcTemplate mysqlJDBC,Long lMainId,long  applyId){

        for(Integer key:mapRowFieldValue.keySet()){

            Map<String,Object> mapFieldItemValues=mapRowFieldValue.get(key);

            //生成SQL语句
            String strSql="insert into "+detailTableName+"("+strFieldNameBuilder.toString()+") \n ";
            strSql+="values("+strFieldParaBuilder.toString()+")";
            List<Object> ListParameter=new ArrayList<>();
            ListParameter.add(lMainId);
            ListParameter.add(applyId);

            for (int i = 0; i < listFields.size(); i++) {
                //QingDownDataFieldDBModel fieldDBModel=listFields.get(i);
                //ListParameter.add(  mapFieldValue.get(  fieldDBModel.getQingTitle()));
                QingDownDataFieldDBModel fieldDBModel=listFields.get(i);

                Object objValue= mapFieldItemValues.get(  fieldDBModel.getDbFieldName());
                ListParameter.add(objValue);
            }
            mysqlJDBC.update(strSql,ListParameter.toArray());

        }


        mapRowFieldValue=new HashMap<>();
        mapFieldValue=new HashMap<>();
    }
}
